<?php

namespace App\Admin\Controllers\Pdf;

use App\Admin\Repositories\Pdf\PdfImage;
use App\Models\Category\Category;
use App\Models\Pdf\PdfImageCoordinate;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PdfImageController extends AdminController
{
    protected $title = '教材点读图片';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PdfImage('category'), function (Grid $grid) {
            $grid->model()->orderBy('real_page');
            $grid->column('id')->sortable();
            $grid->column('category.name', '所属pdf')->display(function ($value) {
                return "<a href='/admin/pdf?id=" . $this->category_id . "'>$value</a>";
            });

            $grid->column('url', '地址')->image();
            $grid->column('width', '图片宽');
            $grid->column('height', '图片高');
            $grid->column('coordinate', '坐标')->display(function ($value) {
                $coordinate_count = PdfImageCoordinate::query()->where('pdf_image_id', $this->id)->count();
                $coordinate_success = PdfImageCoordinate::query()->where('pdf_image_id', $this->id)->whereNotNull('audio')->where('audio', '!=', '')->count();
                return '<span style="color:#409EFF">已设置:' . $coordinate_success . '</span>' . '<span style="color:#F56C6C"> 未设置:' . ($coordinate_count - $coordinate_success) . '</span>' . '<span style="color:#909399"> 全部:' . $coordinate_count . '</span>';
            });
            $grid->column('page', '页面')->editable()->sortable();
            $grid->column('real_page', '真实页面')->editable()->sortable();
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->equal('id')->width(2);
                $filter->like('category.name', '所属pdf名称查询')->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PdfImage(), function (Show $show) {
            $show->field('id');
            $show->field('category_id');
            $show->field('url');
            $show->field('width');
            $show->field('height');
            $show->field('page');
            $show->field('real_page');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PdfImage(), function (Form $form) {
            $form->display('id');
            $form->select('category_id', '所属pdf')->options(Category::query()->where('level', 0)->get()->pluck('name', 'id'))->required();
            $form->number('width', '图片宽')->required();
            $form->number('height', '图片高')->required();
            $form->number('page', '页码')->required();
            $form->number('real_page', '真实页码')->required();
        });
    }
}
